﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.ComponentModel;
using BitmapTagger;
using BrainTechLLC.WPF;
using BrainTechLLC;
using System.Threading;
using System.IO;

namespace ImageAppDataModels
{
    public partial class FileImage : BaseNotifyPropertyChangedUI, INotifyPropertyChanged, IObjectLock, IFileImage
    {
        protected IAdditionalImageInfo _imageInfo;
        protected object _lockImageInfo = new object();

        public IAdditionalImageInfo ImageInfoCached
        {
            get { return _imageInfo; }
        }

        public IAdditionalImageInfo ImageInfo
        {
            get
            {
                if (!_deleted && _imageInfo == null)
                {
                    if (_fileName.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || _fileName.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase))
                    {
                        lock (_lockImageInfo)
                        {
                            if (!_deleted && _imageInfo == null)
                            {
                                Interlocked.CompareExchange<IAdditionalImageInfo>(ref _imageInfo, new AdditionalJpegImageInfo(_fileName), null);
                            }
                        }
                    }
                    else
                    {
                        Interlocked.CompareExchange<IAdditionalImageInfo>(ref _imageInfo, new EmptyImageInfo(_fileName), null);
                    }
                }

                return _imageInfo;
            }               
        }

        public void ClearImageInfo()
        {            
            _imageInfo = null;
        }

        public void ClearSize()
        {
            FileSize = 0;
        }

        #region IFileImage Members

        public void DetachEvents()
        {
        }

        public void AttachEvents()
        {         
        }

        #endregion
    }
}
